package com.car.dao.impl;

import com.car.dao.UserCarDao;
import com.car.doamin.Car;
import com.car.doamin.UserCar;
import com.car.utils.JDBCUtil;

import java.util.List;

public class UserCarDaoImpl implements UserCarDao {
    /**
     * 新增
     *
     * @param userCar 实体类对象
     * @return 影响记录数
     */
    @Override
    public int add(UserCar userCar) throws Exception {
        String sql = "insert t_user_car(user_id, car_id) \n " + " VALUE(?,?)";
        return JDBCUtil.update(sql, userCar.getUserId(), userCar.getCarId());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_user_car where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 更改
     *
     * @param userCar 实体类对象
     * @return 影响记录数
     */
    @Override
    public int update(UserCar userCar) throws Exception {
        String sql = "update t_user_car set user_id = ? \n " + " where id = ?";
        return JDBCUtil.update(sql, userCar.getUserId(), userCar.getCarId(), userCar.getId());
    }

    /**
     * 查询
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public UserCar queryById(int id) throws Exception {
        String sql = "select * from t_user_car where id = ?";
        return JDBCUtil.query(sql, UserCar.class, id);
    }

    /**
     * 查询所有数据
     *
     * @return 查询到的对象
     */
    @Override
    public List<UserCar> queryAll() throws Exception {
        String sql = "select * from t_user_car";
        return JDBCUtil.queryList(sql, UserCar.class);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public List<Car> ownCars(Integer userId) throws Exception {
        String sql = "select * from t_user_car tuc left join t_car tc"+" on tuc.car_id = tc.id where tuc.user_id = ?";

        return JDBCUtil.queryList(sql,Car.class,userId);
    }
}
